
--词条星级	词条上限	单词条几率	品质影响    分解想强化石
local tz_list = {
    ['T1'] = {1,1,5,1,1},
    ['T2'] = {2,2,8,1,2},
    ['T3'] = {3,3,10,1,3},
}

--难度 权重 key 类型，名字
local item_info = {
    {
        {1,50,'T1-1','护手','硬皮护手'},
        {1,50,'T1-2','靴子','硬皮战靴'},
        {2,50,'T1-3','头盔','青铜战盔'},
        {2,50,'T1-4','胸甲','革质胸甲'},
        {3,70,'T1-5','武器','莹石宝剑'},
        {3,10,'T1-6','宠物','树人'},
        {3,10,'T1-7','宠物','狼灵'},
        {3,10,'T1-8','宠物','小冰龙'},
    },{
        {4,50,'T2-1','护手','焰钢护手'},
        {4,50,'T2-2','靴子','焰钢靴子'},
        {5,50,'T2-3','头盔','焰钢头盔'},
        {5,50,'T2-4','胸甲','焰钢胸甲'},
        {6,70,'T2-5','武器','光咒宝剑'},
        {6,10,'T2-6','宠物','树人'},
        {6,10,'T2-7','宠物','狼灵'},
        {6,10,'T2-8','宠物','小冰龙'},
    },{
        {7,50,'T3-1','护手','龙鳞护手'},
        {7,50,'T3-2','靴子','龙鳞靴子'},
        {8,50,'T3-3','头盔','龙鳞头盔'},
        {8,50,'T3-4','胸甲','龙鳞胸甲'},
        {9,70,'T3-5','武器','心徽宝剑'},
        {9,10,'T3-6','宠物','树人'},
        {9,10,'T3-7','宠物','狼灵'},
        {9,10,'T3-8','宠物','小冰龙'},
    }
}

--权重表
local weight_list = {}
local item_map = {}
for _,da in ipairs(item_info) do
    for _,item in ipairs(da) do
        local hard = item[1]
        local weight = item[2]
        local key = item[3]
        local class = item[4]
        local tz = key:match('(T%d)-(.*)')
        item_map[key] = {
            class = class,
            name = ('%s-%s'):format(tz, item[5]),
            tz = tz,
        }
        if weight_list[hard]==nil then
            weight_list[hard] = {}
        end
        table.insert(weight_list[hard],{key,weight})
    end
end

--[随机装备]
function random_name(tab)
	local obj
	if not tab or #tab == 0 then
		return obj
	end

	local pr_total = 0
	for _, v in ipairs(tab) do
		pr_total = pr_total + v[2]
	end

	if pr_total <= 0 then
		return obj
	end

	local random_num = math.random(1, pr_total)
	local pr_hit = 0
	local index = 0
	for i, v in ipairs(tab) do
		if pr_hit <= random_num and random_num <= pr_hit + v[2] then
			index = i
			obj = v[1]
			break
		end
		pr_hit = pr_hit + v[2]
	end

	return obj, index
end

--[随机词条]
local abi_list = {15,17,8,}
local function random_affix(tz,pz)
    local tzdata = tz_list[tz]
    local affix_maxxx = tzdata[1]
    local affix_count = tzdata[2]
    local affix_odds = tzdata[3]
    local affix_odds2 = tzdata[4]
    local odds = (affix_odds + (pz - 1) * affix_odds2)
    local affix = {}
    for a=1,affix_count do
        local rdm = math.random(100)
        if a<=affix_count and rdm<=odds then
            local xx = math.random(1,affix_maxxx)
            local id = math.random(1,abi_list[xx])
            table.insert(affix,xx*1000 + id)
        else
            table.insert(affix,0)
        end
    end
    return table.concat(affix,',')
end

--[解析词条]
local function parser_affix(affix)
    local pattern = ','
    local pos = 0
    local start, stop
    local r = {}
    while true do
        start, stop = affix:find(pattern, pos)
        if not start then
            break
        end
        local id = affix:sub(pos, start - 1)
        table.insert(r, tonumber(id))
        pos = stop + 1
        if maxTokens then
            maxTokens = maxTokens - 1
            if maxTokens == 0 then
                break
            end
        end
    end
    table.insert(r, affix:sub(pos))
    return r
end


--[构造装备]
local function build_item(code)
    local id,abi,time = code:match('%[(%d+)%]BS%-(%d+)-(%d+)')
    if id and abi and time then
        local info = {}
        info.id = id
        info.abi = abi
        info.class = '宝石'
        info.code = code
        info.time = time
        return info
    end
    local id,key,pz,time,affix = code:match('%[(%d+)%](T%d+-%d+)-(%d+)-(%d+)%((.*)%)')
    local info = item_map[key] or {}
    local affixs = parser_affix(affix or '')
    local tzdata = tz_list[info.tz] or {}

    info.id = id
    info.code = code
    info.price = tzdata[5]
    info.pz = pz
    info.time = time
    info.affix = affixs
    
    return info
end

--[创建装备]
function create_item(id,hard)
    local list = weight_list[hard] or weight_list[#weight_list]
    local key = random_name(list)
    local info = item_map[key]
    local pz = math.random(1, 4)  --品质
    local time = os.time()
    local affix = random_affix(info.tz,pz)
    local code = ('[%s]%s-%s-%s(%s)'):format(id, key, pz, time, affix)
    local item = build_item(code)
    return item
end

--[创建宝石]
function create_rock(id,abi)
    local time = os.time()
    local code = ('[%s]BS-%s-%s'):format(id, abi, time)
    local item = build_item(code)
    return item
end


local test = function()
    local str = create_item(100,2).code

    local new = build_item(str)
    print(str)
    for k,v in pairs(new) do
        print(k,v)
    end
end

--test()
return {
    create_item = create_item,
    create_rock = create_rock,
    build_item = build_item,
}